package com.XinYun.Library.modules.admin.borrowMsg.controller;

import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.XinYun.Library.modules.admin.borrowMsg.entity.BorrowMsg;
import com.XinYun.Library.modules.admin.borrowMsg.entity.BorrowPrintMsg;
import com.XinYun.Library.modules.admin.common.AdminReturnCode;
import com.XinYun.Library.modules.admin.borrowMsg.service.BorrowMsgService;
import com.XinYun.Library.modules.admin.common.JSON.AdminJSON;
import com.XinYun.Library.modules.commons.BasicJson;
import com.XinYun.Library.modules.commons.BasicServlet;
import com.XinYun.Library.utils.HTTPRequestUtils.HTTPRequestUtil;
import com.XinYun.Library.utils.JsonUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Date;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@WebServlet("/api/borrowMsg/*")
public class BorrowMsgServlet extends BasicServlet {

    private static BorrowMsgService borrowMsgService = new BorrowMsgService();

    /**
     *
     * @param req
     * @param resp
     * @param methodName
     * @throws IOException
     * @throws NoSuchMethodException
     * @throws ServletException
     */
    @SuppressWarnings("AlibabaSwitchStatement")
    private void borrowMsgDistributor(HttpServletRequest req, HttpServletResponse resp, String methodName) throws IOException, NoSuchMethodException, ServletException {
        //设置返回JSON
        AdminJSON json = new AdminJSON();
        //解析接收JSON
        JSONObject jsonObj = JSONUtil.parseObj(HTTPRequestUtil.getBody(req));
        //方法分配
        switch (methodName){
            case "getRecord": borrowMsgService.getMsg(json,jsonObj);break;
            case "getExcel":  borrowMsgService.getExcel(resp,jsonObj);return;
            default : throw new NoSuchMethodException("没有该方法");
        }
        //发送
        sendJson(json,resp);
    }

    /**
     * 获取存留书籍记录 -分页
     */
    public void getRecord(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, NoSuchMethodException {
        borrowMsgDistributor(req,resp,"getRecord");
    }

    public void getExcel(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, NoSuchMethodException {
        //解析接收JSON
        borrowMsgDistributor(req,resp,"getExcel");
    }
}
